{% extends "member/layout.html" %}

{% block content %}
<div class="card shadow-sm">
    <div class="card-header bg-warning text-dark">
        <h5 class="mb-0">
            <i class="bi bi-pencil-square"></i> 编辑项目
        </h5>
    </div>
    <div class="card-body">
        <form method="post">
            <input name="csrf_token" type="hidden" value="{{ csrf_token() }}">
            {{ form.csrf_token }}

            <!-- 项目状态提示 -->
            <div class="alert alert-info">
                <i class="bi bi-info-circle"></i>
                当前项目状态:
                <span class="badge bg-{{ 'secondary' if project.status == 'draft' else 'danger' }}">
                    {{ '草稿' if project.status == 'draft' else '已拒绝' }}
                </span>
                - 编辑后项目将重置为草稿状态
            </div>

            <!-- 基本信息 -->
            <div class="row mb-3">
                <div class="col-md-6">
                    <label for="name" class="form-label">项目名称 <span class="text-danger">*</span></label>
                    <input type="text" class="form-control" id="name" name="name"
                           value="{{ form.name.data or project.name }}" required>
                    {% if form.name.errors %}
                    <div class="text-danger small">
                        {% for error in form.name.errors %}
                        {{ error }}
                        {% endfor %}
                    </div>
                    {% endif %}
                </div>
                <div class="col-md-6">
                    <label for="category_id" class="form-label">项目类别 <span class="text-danger">*</span></label>
                    <select class="form-select" id="category_id" name="category_id" required>
                        <option value="">-- 选择类别 --</option>
                        {% for category in categories %}
                        <option value="{{ category.id }}"
                                {{ 'selected' if category.id == project.category_id }}>
                            {{ category.name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
            </div>

            <!-- 项目级别和审批流程 -->
            <div class="row mb-3">
                <div class="col-md-6">
                    <label for="level_id" class="form-label">项目级别 <span class="text-danger">*</span></label>
                    <select class="form-select" id="level_id" name="level_id" required>
                        <option value="">-- 选择级别 --</option>
                        {% for level in levels %}
                        <option value="{{ level.id }}"
                                style="color: {{ level.color }};"
                                {{ 'selected' if level.id == project.level_id }}>
                            {{ level.name }} (优先级: {{ level.priority }})
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-6">
                    <label for="approval_flow_id" class="form-label">审批流程 <span class="text-danger">*</span></label>
                    <select class="form-select" id="approval_flow_id" name="approval_flow_id" required>
                        <option value="">-- 选择流程 --</option>
                        {% for flow in flows %}
                        <option value="{{ flow.id }}"
                                {{ 'selected' if flow.id == project.approval_flow_id }}>
                            {{ flow.name }} ({{ flow.department.name if flow.department else '通用' }})
                        </option>
                        {% endfor %}
                    </select>
                </div>
            </div>

            <!-- 审批人和日期 -->
            <div class="row mb-3">
                <div class="col-md-6">
                    <label for="first_approver_id" class="form-label">第一审批人 <span class="text-danger">*</span></label>
                    <select class="form-select" id="first_approver_id" name="first_approver_id" required>
                        <option value="">-- 选择审批人 --</option>
                        {% for approver in approvers %}
                        <option value="{{ approver.id }}"
                                {{ 'selected' if approver.id == project.first_approver_id }}>
                            {{ approver.name }} ({{ approver.department.name }})
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-6">
                    <label for="start_date" class="form-label">开始日期 <span class="text-danger">*</span></label>
                    <input type="date" class="form-control" id="start_date" name="start_date"
                           value="{{ project.started_at.strftime('%Y-%m-%d') if project.started_at }}" required>
                </div>
            </div>

            <!-- 结束日期 -->
            <div class="row mb-3">
                <div class="col-md-6">
                    <label for="end_date" class="form-label">结束日期 <span class="text-danger">*</span></label>
                    <input type="date" class="form-control" id="end_date" name="end_date"
                           value="{{ project.end_date.strftime('%Y-%m-%d') if project.end_date }}" required>
                </div>
            </div>

            <!-- 项目描述 -->
            <div class="mb-3">
                <label for="description" class="form-label">项目描述</label>
                <textarea class="form-control" id="description" name="description" rows="3">{{ form.description.data or project.description or '' }}</textarea>
                {% if form.description.errors %}
                <div class="text-danger small">
                    {% for error in form.description.errors %}
                    {{ error }}
                    {% endfor %}
                </div>
                {% endif %}
            </div>

            <!-- 项目成员 -->
            <div class="mb-4">
                <label class="form-label">项目成员 <span class="text-danger">*</span></label>
                <div class="alert alert-warning small">
                    <i class="bi bi-exclamation-triangle"></i>
                    项目负责人（{{ current_member.name }}）会自动包含在成员列表中
                </div>
                <div class="row">
                    {% for member in members %}
                    <div class="col-md-4 mb-2">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox"
                                   name="member_ids" value="{{ member.id }}"
                                   id="member{{ member.id }}"
                                   {{ 'checked' if member.id in form.member_ids.data }}>
                            <label class="form-check-label" for="member{{ member.id }}">
                                {{ member.name }}
                                <small class="text-muted">({{ member.department.name }})</small>
                                {% if member.id == current_member.id %}
                                <span class="badge bg-primary ms-1">负责人</span>
                                {% endif %}
                            </label>
                        </div>
                    </div>
                    {% endfor %}
                </div>
                {% if form.member_ids.errors %}
                <div class="text-danger small">
                    {% for error in form.member_ids.errors %}
                    {{ error }}
                    {% endfor %}
                </div>
                {% endif %}
            </div>

            <!-- 操作按钮 -->
            <div class="d-flex justify-content-between">
                <div>
                    <button type="submit" class="btn btn-warning">
                        <i class="bi bi-check-circle"></i> 更新项目
                    </button>
                    <a href="{{ url_for('member.jira.project_detail', project_id=project.id) }}" class="btn btn-secondary">
                        <i class="bi bi-x-circle"></i> 取消
                    </a>
                </div>
                <div>
                    <a href="{{ url_for('member.jira.list_projects') }}" class="btn btn-outline-primary">
                        <i class="bi bi-list-ul"></i> 返回列表
                    </a>
                </div>
            </div>
        </form>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
// 页面加载完成后设置日期字段的最小值
document.addEventListener('DOMContentLoaded', function() {
    const startDateInput = document.getElementById('start_date');
    const endDateInput = document.getElementById('end_date');

    // 设置开始日期的最小值为今天
    const today = new Date().toISOString().split('T')[0];
    startDateInput.min = today;

    // 当开始日期变化时，更新结束日期的最小值
    startDateInput.addEventListener('change', function() {
        endDateInput.min = this.value;
    });

    // 初始化结束日期的最小值
    if (startDateInput.value) {
        endDateInput.min = startDateInput.value;
    }
});
</script>
{% endblock %}